import React from 'react';
import {Button, Form, Input, Row, Col, message} from 'antd';
import qs from 'qs';
import * as Constant from '../../../../common/const';
import * as Request from '../../../../Server/Upgrade';

const FormItem = Form.Item;

class CollectInfosForm extends React.Component {
    constructor(props) {
        super(props);
    }

    handleSubmit = (e) => {
        e.preventDefault();
        this.props.form.validateFieldsAndScroll((err, values) => {
            if (!err) {
                const request = values.id ? Request.collectInfosUpdate : Request.collectInfosSave;
                values.idMain = values.idMain.replace(/\s*/g,"");
                values.enCodeRule = values.enCodeRule.replace(/\s*/g,"");
                request(qs.stringify(values)).then(res => {
                    if (Constant.REQUEST_SUCCESS == res.code) {
                        message.success(Constant.OPERATION_SUCCESS_TIP, Constant.AUTO_DISAPPEAR_TIME);
                        this.props.cancel(true);
                    }
                });
            }
        });
    }
    validator = (rule, value, callback) => {
        const id = this.props.form.getFieldValue('id');
        let params = {idMain: value};
        id && (params.id = id);
        if(value && value.length){
            Request.collectInfosValidator(qs.stringify(params)).then(res => {
                if (Constant.REQUEST_SUCCESS == res.code) {
                    if (res.data) {
                        callback();
                    } else {
                        callback('验证不通过');
                    }
                } else {
                    callback(res.msg);
                }
            })
        }else{
            callback();
        }
    }

    render() {
        const {getFieldDecorator} = this.props.form;
        const ipFormItemLayout = {
            labelCol: {
                xs: {span: 24},
                sm: {span: 6},
            },
            wrapperCol: {
                xs: {span: 24},
                sm: {span: 15},
            },
        };
        const tailFormItemLayout = {
            wrapperCol: {
                xs: {
                    span: 24,
                    offset: 0,
                },
                sm: {
                    span: 16,
                    offset: 8,
                },
            },
        };

        return (
            <div>
                <Form onSubmit={this.handleSubmit}>
                    <FormItem>
                        {getFieldDecorator('id', {
                            initialValue: this.props.collectInfos && this.props.collectInfos.id,
                        })(
                            <Input type={'hidden'}/>
                        )}
                    </FormItem>
                    <FormItem label={'类别:'} {...ipFormItemLayout} >
                        {getFieldDecorator('idMain', {
                            rules: [{
                                required: true, message: '请输入类别!',
                            },{
                                validator: this.validator,
                            }],
                            initialValue: this.props.collectInfos && this.props.collectInfos.idMain,
                        })(
                            <Input maxLength={255}/>
                        )}
                    </FormItem>
                    <FormItem label={'编码规范:'} {...ipFormItemLayout} >
                        {getFieldDecorator('enCodeRule', {
                            rules: [{
                                required: true, 
                                message: '请输入编码规范！',
                            }],
                            initialValue: this.props.collectInfos && this.props.collectInfos.enCodeRule,
                        })(
                            <Input maxLength={255}/>
                        )}
                    </FormItem>
                    <FormItem {...tailFormItemLayout}>
                        <Button type="primary" htmlType="submit">保存</Button>
                        <Button type="primary" onClick={this.props.cancel}>返回</Button>
                    </FormItem>
                </Form>
            </div>
        );
    }
}

const RegisterCollectInfosForm = Form.create()(CollectInfosForm);
export default RegisterCollectInfosForm;